import {
	BASEDOC
} from '@/common/const/index';

/**
 * 缓存不为空的数据
 * @param {Object} key 键
 * @param {Object} data 值
 */
function setNotNullDataStorage(key, data) {
	if (Array.isArray(data)) {
		uni.setStorage({
			key: key,
			data: data
		});
	}
}

/**
 * 下载全部的mes档案，并将下载的档案缓存到本地
 * @param {Object} i 开始下标，从0开始
 */
export const downloadAllMes2Doc = function(i) {
	if (i < mesBasedocArrs.length) {
		setTimeout(() => {
			if (mesBasedocArrs[i].key) {
				downloadOneMes2Doc.call(this, mesBasedocArrs[i]);
			}
			// 继续递归调用下载
			downloadAllMes2Doc.call(this, i + 1);
		}, 500)
	} else {
		this.$tool.msg('档案已下载完成！')
	}
}

/**
 * @param {Object} item 需要下载的档案信息
 * 发送档案下载请求，并将下载的档案缓存到本地
 */
export const downloadOneMes2Doc = function(item) {
	let that = this;
	let queryData = {};
	let pk_org = '0001A110000000026XCR';
	
	if (item.key == BASEDOC.FIXEDPOSTSTAFF) {
		this.$api
			.queryFixedPostStaff(queryData)
			.then((res) => {
				let PostStaffApiSdt = res.PostStaffApiSdt;
				let refArrs = [];
				PostStaffApiSdt.map((tempItem) => {
					let tempObj = {};
					tempObj.refid = tempItem.pk_postStaff;
					tempObj.refname = tempItem.psndocName;
					tempObj.refcode = tempItem.psndocCode;
		
					refArrs.push(Object.assign({}, tempObj, tempItem));
				});
				setNotNullDataStorage(item.key, refArrs)
				that.$tool.msg(`下载成功，共下载${refArrs.length}条数据`);
			})
			.catch((error) => {
				that.$tool.msg(`${item.key}请求失败，${error}`)
			});
	}
	if (item.key == BASEDOC.WorkProcedure || item.key == BASEDOC.ClassGroup) {
		queryData.pk_org = pk_org;
		queryData.basedocType = item.key;
		this.$api
			.queryBasedocList(queryData)
			.then((res) => {
				let { data, success } = res;
				setNotNullDataStorage(item.key, data)
				that.$tool.msg(`下载成功，共下载${data.length}条数据`);
			})
			.catch((error) => {
				that.$tool.msg(`${item.key}请求失败，${error}`)
			});
	}
	if (item.key == BASEDOC.ORG) {
		this.$api
			.queryOrgs(queryData)
			.then((res) => {
				let OrgSdtItemes = res.OrgSdtItemes;
				let refArrs = [];
				OrgSdtItemes.map((tempItem) => {
					let tempObj = {};
					tempObj.refid = tempItem.Id;
					tempObj.refname = tempItem.Name;
					tempObj.refcode = tempItem.Name;
					refArrs.push(tempObj);
				});
				setNotNullDataStorage(item.key, refArrs)
				that.$tool.msg(`下载成功，共下载${refArrs.length}条数据`);
			})
			.catch((error) => {
				that.$tool.msg(`${item.key}请求失败，${error}`)
			});
	}
	if (item.key == BASEDOC.DEPT) {
		if (this.currentUserInfo) {
			// queryData.pk_org = this.currentUserInfo.pk_org;
		}
		queryData.pk_org = pk_org;
		this.$api
			.queryDepts(queryData)
			.then((res) => {
				let DeptSdt4ApiItems = res.DeptSdt4ApiItems;
				let refArrs = [];
				DeptSdt4ApiItems.map((tempItem) => {
					let tempObj = {};
					tempObj.refid = tempItem.id;
					tempObj.refname = tempItem.name;
					tempObj.refcode = tempItem.code;
					refArrs.push(Object.assign({}, tempObj, tempItem));
				});
				setNotNullDataStorage(item.key, refArrs)
				that.$tool.msg(`下载成功，共下载${refArrs.length}条数据`);
			})
			.catch((error) => {
				that.$tool.msg(`${item.key}请求失败，${error}`)
			});
	}
	if (item.key == BASEDOC.PSNDOC) {
		this.$api
			.queryPsndoc(queryData)
			.then((res) => {
				let PsndocSdt4ApiItems = res.PsndocSdt4ApiItems;
				let refArrs = [];
				PsndocSdt4ApiItems.map((tempItem) => {
					let tempObj = {};
					tempObj.refid = tempItem.id;
					tempObj.refname = tempItem.name;
					tempObj.refcode = tempItem.code;
	
					refArrs.push(Object.assign({}, tempObj, tempItem));
				});
				that.$tool.msg(`下载成功，共下载${refArrs.length}条数据`);
				uni.setStorage({
					key: item.key,
					data: refArrs,
					success: function (e) {
						let userData = uni.getStorageSync(BASEDOC.SM_USER);
						let mergeData = refArrs.concat(userData);
						uni.setStorage({
							key: BASEDOC.PSNDOC_USER,
							data: mergeData
						});
					}
				});
			})
			.catch((error) => {
				that.$tool.msg(`${item.key}请求失败，${error}`)
			});
	}
	if (item.key == BASEDOC.MATERIAL) {
	}
	if (item.key == BASEDOC.SM_USER) {
		this.$api
			.queryUser(queryData)
			.then((res) => {
				if (res && res.length != 0) {
					let refArrs = [];
					res.map((tempItem) => {
						let tempObj = {};
						tempObj.refid = tempItem.GUID;
						tempObj.refname = tempItem.LastName;
						tempObj.refcode = tempItem.Name;
						refArrs.push(tempObj);
					});
					if (Array.isArray(refArrs)) {
						uni.setStorage({
							key: item.key,
							data: refArrs,
							success: function (e) {
								let psndocData = uni.getStorageSync(BASEDOC.PSNDOC);
								let mergeData = refArrs.concat(psndocData);
								uni.setStorage({
									key: BASEDOC.PSNDOC_USER,
									data: mergeData
								});
							}
						});
					}
				}
				that.$tool.msg(`下载成功，共下载${res.length}条数据`);
			})
			.catch((error) => {
				that.$tool.msg(`${item.key}请求失败，${error}`)
			});
	}
};

/**
 * mes 档案数组，有新的档案时，需要在这个数组加上
 */
export const mesBasedocArrs = [{
		text: "组织",
		btn: "下载",
		key: `${BASEDOC.ORG}`,
		field: [
			"code",
			"name",
			"pk_stockorg"
		],
		refid: "pk_stockorg"
	},
	{
		text: "部门",
		btn: "下载",
		key: `${BASEDOC.DEPT}`,
		field: [
			"code",
			"name",
			"pk_stockorg"
		],
		refid: "pk_stockorg"
	},
	{
		text: "工序",
		btn: "下载",
		key: `${BASEDOC.WorkProcedure}`,
		field: [
			"code",
			"name",
			"pk_stockorg"
		],
		refid: "pk_stockorg"
	},
	{
		text: "班组",
		btn: "下载",
		key: `${BASEDOC.ClassGroup}`,
		field: [
			"code",
			"name",
			"pk_stockorg"
		],
		refid: "pk_stockorg"
	},
	{
		text: "人员",
		btn: "下载",
		key: `${BASEDOC.PSNDOC}`,
		field: [
			"code",
			"name"
		],
		refid: "pk_psndoc"
	},
	{
		text: "用户",
		btn: "下载",
		key: `${BASEDOC.SM_USER}`,
		field: [
			"code",
			"name"
		],
		refid: "guid"
	},
	{
		text: "定岗定编",
		btn: "下载",
		key: `${BASEDOC.FIXEDPOSTSTAFF}`,
		field: [
			"code",
			"name"
		],
		refid: "guid"
	}
];